package com.xyebank.ledger.service.lkl.adapter;

import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LPaymentInf;
import com.xyebank.ledger.service.common.conveter.PayBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.util.StatusConvertUtil;
import com.xyebank.ledger.vo.deal.LKLCashInfVo;

// TODO
// finish
@Component(value = "lklPayBeanConverter")
public class LKLPayBeanConverter implements PayBeanConverter<LKLCashInfVo> {
    
    // 不能采取注入方式
    private DateConverter dateConverter = new DateConverter();
    
    ModelMapper modelMapper = new ModelMapper();

    class LKLPayPropertyMap extends PropertyMap<LKLCashInfVo, LPaymentInf> {
        protected void configure() { //注意　不要有重复的映射，不然会报错
            map(source.getContractNo(), destination.getOrgOrderNo());
            map(source.getMblNo(), destination.getMblNo());
            map(source.getApplyAmount(), destination.getApplyAmount());
            using(dateConverter).map(source.getApplyTime(), destination.getApplyTime());
            using(dateConverter).map(source.getCompleteTime(), destination.getApprovalTime());
            map(source.getLoanAmount(), destination.getLoanAmount());
            map(source.getLoanTime(), destination.getLoanTime());
            map(source.getProductCycle(), destination.getLoanPeriod());
            
        }
    }

    public LKLPayBeanConverter() {
        modelMapper.addMappings(new LKLPayPropertyMap());
    }

    @Override
    public LPaymentInf covert(LKLCashInfVo x) {
        LPaymentInf entity = modelMapper.map(x, LPaymentInf.class);
        entity.setId(IDUtil.generateId(x.getId().toString(), "lkl"));
        entity.setDataSource(x.getChannel().equals("YDSJD2") ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
        entity.setOrgNo(ProductConstant.LKL_LOAN_ORG);
        entity.setProductNo(ProductConstant.LKL_LOAN_PROD_ID);
        entity.setApprovalStatus(StatusConvertUtil.convertApplyStatus(x.getContractStatus()));
        return entity;
    }
}
